package de.tum.in.softwareengineering1.ws201112.team11.sudoku.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.MatteBorder;

import de.tum.in.softwareengineering1.ws201112.team11.sudoku.game.SudokuGame;
import de.tum.in.softwareengineering1.ws201112.team11.sudoku.util.Constants;


public class SudokuGUI extends JFrame {

	private JButton			createNewGame;
	private JButton			checkSolution;
	private JButton			showSolution;
	private JButton			reset;
	private JMenuBar		menuBar;																		
	private JTextField[][]	textFields	= new JTextField[Constants.FIELD_LENGTH][Constants.FIELD_LENGTH];
	
	private SudokuGame		game;
	private SudokuGame		originalGame;
	
	private JMenuItem	newGameItem;
	private JMenu	settingsMenu;
	private JMenu	actionsMenu;
	private JMenuItem	solveGameItem;
	private JMenuItem	checkSolutionItem;

	public SudokuGUI() throws Exception {
		this.game = new SudokuGame();
		this.originalGame = game;
		this.getContentPane().setLayout(null);
		this.initGame();
		this.setVisible(true);

	}

	private void createNewGame() throws Exception {
		game.randomize();
		updateFields();
	}

	private void updateFields() {
		for (int i = 0; i < Constants.FIELD_LENGTH; i++) {
			for (int j = 0; j < Constants.FIELD_LENGTH; j++) {
				textFields[i][j].setText(Integer.toString(game.getField()[i][j]));
			}
		}
	}

	private void initTextFields() {
		for (int i = 0; i < Constants.FIELD_LENGTH; i++) {
			for (int j = 0; j < Constants.FIELD_LENGTH; j++) {
				textFields[i][j] = new JTextField();
				textFields[i][j].setFont(new Font(Font.SANS_SERIF, 1, 18));
				createTextfieldBorders(i, j);
				textFields[i][j].setHorizontalAlignment(JTextField.CENTER);
				textFields[i][j].setBounds(Constants.LEFT_DISTANCE + i * Constants.INPUT_TEXT_FIELD_SIZE, Constants.TOP_DISTANCE + j * Constants.INPUT_TEXT_FIELD_SIZE, Constants.INPUT_TEXT_FIELD_SIZE, Constants.INPUT_TEXT_FIELD_SIZE);
				this.getContentPane().add(textFields[i][j]);
				
			}
		}
	}

	/**
	 * @param i
	 * @param j
	 * @author Markus
	 * @version 
	 * @time 04.01.2012
	 */
	private void createTextfieldBorders(int i, int j) {
		if(j == 2 || j == 5){
			if(i == 2 || i == 5){
				textFields[i][j].setBorder(new MatteBorder(1, 1, 3, 3, Color.BLACK));
			}
			else{
			
			textFields[i][j].setBorder(new MatteBorder(1, 1, 3, 1, Color.BLACK));
			}

		}
		else{
			
			if(i == 2 || i == 5){
				textFields[i][j].setBorder(new MatteBorder(1, 1, 1, 3, Color.BLACK));
			}
			else{
			
			textFields[i][j].setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
			}
		}
	}

	public void initGame() {

		initTextFields();

		initNewGameButton();
		initMenu();
		initActionListener();
		
	
		this.setTitle("Sudoku - Team 11");
		this.setResizable(true);
		this.setMinimumSize(new Dimension(600, 400));
		
		this.pack();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

	/**
	 * 
	 * @author Markus
	 * @version 
	 * @time 04.01.2012
	 */
	private void initMenu() {
		newGameItem = new JMenuItem("New Game");
		solveGameItem = new JMenuItem("Solve Game");
		checkSolutionItem = new JMenuItem("Check Solution");
		
		menuBar = new JMenuBar();
		settingsMenu = new JMenu("Settings");
		actionsMenu = new JMenu("Actions");
		
		
		actionsMenu.add(newGameItem);
		actionsMenu.add(solveGameItem);
		actionsMenu.add(checkSolutionItem);
		
		menuBar.add(actionsMenu);
		menuBar.add(settingsMenu);
		
		this.setJMenuBar(menuBar);
	}
	
	private void initActionListener() {
		newGameItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				try {
					
					/**
					 * TODO
					 */
					createNewGame();
				}
				catch (Exception e) {
					e.printStackTrace();
				}

			}

		});
		solveGameItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				try {
					/**
					 * TODO
					 */
					createNewGame();
				}
				catch (Exception e) {
					e.printStackTrace();
				}

			}

		});
		
		checkSolutionItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				try {
					
					/**
					 * TODO
					 */
					createNewGame();
				}
				catch (Exception e) {
					e.printStackTrace();
				}

			}

		});
		

	}

	/**
	 * 
	 * @author Markus
	 * @version
	 * @time 03.01.2012
	 */
	private void initNewGameButton() {
		createNewGame = new JButton("Create New Game");
		createNewGame.setBounds(350, 20, 150, 20);
		createNewGame.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				try {
					createNewGame();
				}
				catch (Exception e) {
					e.printStackTrace();
				}

			}

		});

		this.getContentPane().add(createNewGame);
	}
}
